package com.bawei.module_launcher.gateway

import androidx.lifecycle.viewModelScope
import com.bawei.library_mvi.model.BaseViewModel
import com.bawei.library_base.R
import com.bawei.module_launcher.gateway.entity.GatewayEntity
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import java.util.ArrayList

/**
 * @Author : 
 * @Time : On 2023/7/20 15:42
 * @Description : GateWayViewModel
 */
class GateWayViewModel:BaseViewModel<GateWayIntent>() {
    //UIState
    private val _states = MutableStateFlow<GateWayState>(GateWayState.Init)//初始化默认的状态
    val state: StateFlow<GateWayState>
        get() = _states

    //处理意图
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is GateWayIntent.DataIntent -> getGateWayData()//请求数据
                }
            }
        }

    }

    private val iconsArray = arrayOf(R.drawable.icon32,R.drawable.icon32,R.drawable.icon140,
        R.drawable.icon829,R.drawable.icon359,
        R.drawable.icon1353,R.drawable.icon3)
    private val titleArray = arrayOf("在线应用","本地应用","考勤打卡","智能审批","工作日志",
        "任务","文件")
    private val pathArray = arrayOf("/apps/NetAppsActivity",
        "/apps/LocalAppsActivity",
        "/attendance/signactivity",
        "智能审批","工作日志",
        "任务","文件")
    private val iconsIMArray = arrayOf(R.drawable.icon1887,R.drawable.icon1622,
        R.drawable.icon1904)
    private val titleIMArray = arrayOf("联系人","讨论组","组织架构")
    private val pathIMArray = arrayOf("联系人","讨论组","组织架构")





    //获得数据--》自己本地封装数据
    private fun getGateWayData() {
        val datas = mutableListOf<GatewayEntity>()
        val titleEntity1 = GatewayEntity("应用",R.drawable.icon1382,"",100)
        datas.add(titleEntity1)
        var index = 0
        while (index < iconsArray.size){
            val entity = GatewayEntity(titleArray.get(index),
                iconsArray[index], pathArray[index],200)
            datas.add(entity)
            index++
        }
        val titleEntity2 = GatewayEntity("通信",R.drawable.icon1488,"",100)
        datas.add(titleEntity2)
        index = 0
        while (index < iconsIMArray.size){
            val entity = GatewayEntity(
                titleIMArray[index],
                iconsIMArray[index], pathIMArray[index],200)
            datas.add(entity)
            index++
        }

        //更新state
        viewModelScope.launch {
            _states.value = GateWayState.GateWaySuccessState(datas)
        }

    }




}